package com.example.blog.modules.log.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.modules.log.annotation.Log;
import com.example.blog.modules.log.dto.OperationLogDTO;
import com.example.blog.modules.log.service.OperationLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "操作日志接口", description = "操作日志管理相关接口")
@RestController
@RequestMapping("/api/logs")
@RequiredArgsConstructor
@SecurityRequirement(name = "bearerAuth")
@PreAuthorize("hasRole('ADMIN')")
public class OperationLogController {
    
    private final OperationLogService operationLogService;
    
    @Operation(summary = "获取所有操作日志")
    @GetMapping
    public ApiResponse<Page<OperationLogDTO>> getLogs(Pageable pageable) {
        return ApiResponse.success(operationLogService.findAll(pageable));
    }
    
    @Operation(summary = "按用户名查询操作日志")
    @GetMapping("/user/{username}")
    public ApiResponse<Page<OperationLogDTO>> getLogsByUsername(
            @PathVariable String username,
            Pageable pageable) {
        return ApiResponse.success(operationLogService.findByUsername(username, pageable));
    }
    
    @Operation(summary = "按操作类型查询操作日志")
    @GetMapping("/operation/{operation}")
    public ApiResponse<Page<OperationLogDTO>> getLogsByOperation(
            @PathVariable String operation,
            Pageable pageable) {
        return ApiResponse.success(operationLogService.findByOperation(operation, pageable));
    }
    
    @Operation(summary = "删除操作日志")
    @Log(value = "删除操作日志", operation = "DELETE")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteLog(@PathVariable Long id) {
        operationLogService.deleteById(id);
        return ApiResponse.success();
    }
    
    @Operation(summary = "批量删除操作日志")
    @Log(value = "批量删除操作日志", operation = "DELETE")
    @DeleteMapping("/batch")
    public ApiResponse<Void> deleteLogBatch(@RequestBody Long[] ids) {
        operationLogService.deleteByIds(ids);
        return ApiResponse.success();
    }
} 